<template>
    <view>
        <!-- pages/inviteVisitor/inviteVisitor.wxml -->
        <view>
            <image class="mid-icon" src="../../static/assets/img/invite.png"></image>
            <view class="box-item" @tap="showSelectbox">
                <view class="box-item-text">来访小区</view>
                <view class="select-name">
                    {{ parkingName }}
                    <image src="../../static/assets/img/arrow.png" class="box-img-arrow"></image>
                </view>
            </view>
            <view class="box-line"></view>
            <view class="box-tips">由于小区安全管理需要，同时方便来访客人停车，访客车辆进入小区需提前预约。 请点击下方“邀请”按钮，将链接发给客人微信进行预约：</view>
            <!-- <view class="input-title">请选择小区名称</view>
        <view class="select-box" bindtap="showSelectbox">
            <view wx:if="{{parkingName == ''}}">请选择<image src="/assets/img/dropdpwn.png"></image></view>
            <view wx:else class="select-name">{{parkingName}}</view>
        </view> -->
            <view><button open-type="share">点击邀请</button></view>
        </view>

        <van-popup v-model="show" position="bottom" @close="onClose">
            <van-picker show-toolbar :columns="list" @cancel="onCancel" @confirm="onConfirm" @change="onChange" />
        </van-popup>
    </view>
</template>

<script>
// pages/inviteVisitor/inviteVisitor.js
import util from './../../utils/util.js';
import api from './../../utils/api.js';
export default {
    data() {
        return {
            list: [],
            show: false,
            parkingName: '',
            parkingCode: '',
            visitorCode: '',
            isDisabled: true
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        this.getUnitSpaceList();
    },
    onShareAppMessage: function (options) {
        console.log(options);
        return {
            title: '访客邀请',
            path: '/pages/blank/blank?parkingCode=' + this.parkingCode + '&visitorCode=' + this.visitorCode,
            //这里拼接需要携带的参数
            // imageUrl: 'https://shareparking.wasu.cn/share-parking/static/shareImg.jpg',
            success: function (res) {
                console.log('转发成功');
            },
            fail: function (err) {
                console.log('转发失败' + err);
            }
        };
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    methods: {
        goInvite: function (options) {
            uni.navigateTo({
                url: '../newParklist/newParklist?parkingcode=' + this.parkingCode + '&active=1'
            });
        },

        // 邀请访客 ----写在share逻辑里
        inviteVisitor() {
            let param = {
                userUnionId: uni.getStorageSync('unionId'),
                parkingCode: this.parkingCode || ''
            };
            util.request(api.inviteVisitor, param, 'GET')
                .then((res) => {
                    if (res.success) {
                        this.setData({
                            visitorCode: res.data
                        });
                    }
                })
                .catch((err) => {
                    console.log(err);
                });
        },

        // 选择小区
        showSelectbox() {
            this.setData({
                show: true
            });
        },

        // 选择框操作
        onClose() {
            this.setData({
                show: false
            });
        },

        onChange(event) {
            const { picker, value, index } = event.detail; // console.log(picker, value, index);
            // Toast(`当前值：${value}, 当前索引：${index}`);
        },

        // 下拉框选择
        onConfirm(e) {
            this.setData({
                parkingName: e.detail.value.parkingName,
                parkingCode: e.detail.value.parkingCode
            });
            this.setData({
                show: false
            });
            this.inviteVisitor(); //生成访客邀请码
        },

        onCancel() {
            this.setData({
                show: false
            });
        },

        //查询关联小区
        getUnitSpaceList() {
            let param = {
                userUnionId: uni.getStorageSync('unionId')
            };
            util.request(api.getUnitspaceList, param, 'GET')
                .then((res) => {
                    if (res.success) {
                        let listdata = [];

                        if (res.data && res.data.length > 0) {
                            res.data.map((item) => {
                                if (item.unitAuthStatus == 1) {
                                    listdata.push(
                                        Object.assign({}, item, {
                                            text: item.parkingName
                                        })
                                    );
                                }
                            });
                        }

                        this.setData({
                            list: listdata,
                            parkingCode: listdata[0].parkingCode,
                            parkingName: listdata[0].parkingName
                        });
                        this.inviteVisitor(); //生成访客邀请码
                    } else {
                        uni.showToast({
                            title: res.msg || '接口出错了...',
                            icon: 'none'
                        });
                    }
                })
                .catch((err) => {
                    console.log(err);
                    uni.showToast({
                        title: '接口出错了',
                        icon: 'none'
                    });
                });
        }
    }
};
</script>
<style>
@import './inviteVisitor.css';
</style>
